package com.yingsheng.bi.hks.service.comm;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.ess.v20201111.EssClient;
import com.tencentcloudapi.ess.v20201111.models.*;
import com.yingsheng.bi.hks.basic.exception.TencentRuntimeException;
import com.yingsheng.bi.hks.dto.ess.EssCreateFlowReq;
import com.yingsheng.bi.hks.dto.ess.EssCreateFlowRes;
import com.yingsheng.bi.hks.dto.ess.EssQryTemplatesReq;
import com.yingsheng.bi.hks.dto.ess.EssQryTemplatesRes;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

/**
 * 电子签服务
 *
 * @author root
 */
@Slf4j
@Service
public class EssBiService implements InitializingBean {

    private EssClient client;

    @Override
    public void afterPropertiesSet() throws Exception {
        Credential cred = new Credential("SecretId", "SecretKey");
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("ess.test.ess.tencent.cn");
        // 实例化一个client选项，可选的，没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        this.client = new EssClient(cred, "", clientProfile);
    }


    public EssQryTemplatesRes describeFlowTemplates(EssQryTemplatesReq req) {
        DescribeFlowTemplatesRequest describeFlowTemplatesRequest = new DescribeFlowTemplatesRequest();
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId("yDCY0UUckp7ou205Uu3DuDLvRNmftnox");
        describeFlowTemplatesRequest.setOperator(userInfo);

        EssQryTemplatesRes res = new EssQryTemplatesRes();
        try {
            client.DescribeFlowTemplates(describeFlowTemplatesRequest);
        } catch (TencentCloudSDKException e) {
            throw new TencentRuntimeException(e.getErrorCode(), e.getMessage());
        }
        return res;
    }


    public EssCreateFlowRes createFlow(EssCreateFlowReq req) {

        CreateFlowRequest createFlowRequest = new CreateFlowRequest();
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId("yDCY0UUckp7ou205Uu3DuDLvRNmftnox");
        createFlowRequest.setOperator(userInfo);
        createFlowRequest.setFlowName(req.getFlowName());

        // 合同流程的参与方列表，最多可支持50个参与方
        FlowCreateApprover flowCreateApprover1 = new FlowCreateApprover();
        flowCreateApprover1.setApproverType(1L);
        FlowCreateApprover flowCreateApprover2 = new FlowCreateApprover();
        flowCreateApprover2.setApproverType(1L);

        // 要求approver在数组中的顺序与模板定义顺序一致
        createFlowRequest.setApprovers(new FlowCreateApprover[]{flowCreateApprover1, flowCreateApprover1});

        EssCreateFlowRes res = new EssCreateFlowRes();
        try {

            CreateFlowResponse createFlowResponse = client.CreateFlow(createFlowRequest);
            res.setFlowId(createFlowResponse.getFlowId());
            res.setRequestId(createFlowResponse.getRequestId());
        } catch (TencentCloudSDKException e) {
            log.error(e.getMessage(), e);
            throw new TencentRuntimeException(e.getErrorCode(), e.getMessage());
        }

        return res;
    }


}
